package com.wash.shoes.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.wash.shoes.domain.vo.CouponVo;
import com.wash.shoes.entity.CouponPo;
import com.wash.shoes.entity.CustomerCouponPo;
import com.wash.shoes.mapper.CouponMapper;
import com.wash.shoes.page.PageFactory;
import com.wash.shoes.service.CouponService;
import com.wash.shoes.service.CustomerCouponService;
import com.wash.shoes.util.HeaderUtil;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 优惠卷 服务实现类
 * </p>
 *
 * @author ljg
 * @since 2024-12-01
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponPo> implements CouponService {

    @Autowired
    CustomerCouponService customerCouponService;

    /**
     * 获取列表的数据
     *
     * @param couponPo 优惠卷
     * @return 列表的数据
     */
    @Override
    public Page<CouponPo> pageList(CouponPo couponPo) {
        Page<CouponPo> page = PageFactory.defaultPage();
        List<CouponPo> dataList = baseMapper.pageList(page, couponPo);
        page.setRecords(dataList);
        return page;
    }

    /**
     * 移动端的首页查询优惠卷的列表
     *
     * @return
     */
    @Override
    public List<CouponVo> showCouponList(String customerId,String channel) {
//        String poId=null;
//        if(StringUtils.isNoneBlank(channel)){
//            poId= HeaderUtil.getPoId();
//        }
        List<CouponVo> dataList=baseMapper.showCouponList(null);
        for (CouponVo couponVo : dataList) {
            if (StrUtil.isNotBlank(customerId)) {
                List<CustomerCouponPo> customerCouponPos = customerCouponService.getUserCouponCount(customerId, couponVo.getId());
                if (customerCouponPos.isEmpty()) {
                    couponVo.setUserHasGetNum(0);
                } else {
                    couponVo.setUserHasGetNum(customerCouponPos.size());
                }
                for (CustomerCouponPo couponPo : customerCouponPos) {
                    if (couponPo.getStatus() == 1) {
                        couponVo.setUserHasUseNum(couponVo.getUserHasUseNum() + 1);
                    }
                }
            }
        }
        return dataList;
    }


}
